(function () {
  const elevator = document.querySelector('.xtx-elevator');

  const backTop = document.querySelector('#backTop');
// 绑定滚动事件
  window.addEventListener('scroll', function () {
    const n = document.documentElement.scrollTop
    // 判断被卷去的距离是否符合条件，
    n >= 300?elevator.style.opacity=1:elevator.style.opacity=0     
  });
  // 绑定顶部点击事件
  backTop.addEventListener('click', function () {
    // 用户一点击就让页面滚动回到顶部
    window.scrollTo(0, 0)
  });
})();

(function () {
  const elevator = document.querySelector('.xtx-elevator');
  const newGoods = document.querySelector('.xtx_goods_new');
  const popular = document.querySelector('.xtx_goods_popular');
  const brand = document.querySelector('.xtx_goods_brand');
  const topic = document.querySelector('.xtx_goods_topic');
  // 绑定事件委托
  elevator.addEventListener('click', function (e) {
    if (e.target.tagName === 'A' && e.target.dataset.name) {
      // 获取自定义属性
      const goodsName = e.target.dataset.name
      // 判断是否符合条件
      switch (goodsName) {
        case 'newGoods':
          // 如果选中是newGoods，让页面滚动到newGoods
          window.scrollTo(0, newGoods.offsetTop)
          break;
        case 'popular':
          window.scrollTo(0, popular.offsetTop)
          break;
        case 'brand':
          window.scrollTo(0, brand.offsetTop)
          break;
        case 'topic':
          window.scrollTo(0, topic.offsetTop)
          break;
        default:
          alert('出BUG了') 
       }
     }
  });
// 绑定滚动事件
  window.addEventListener('scroll', function () {
    // 获取被卷去的距离
    const n = document.documentElement.scrollTop

    elevator.querySelector('a.active') && elevator.querySelector('a.active').classList.remove('active')
    if (n >= newGoods.offsetTop && n < popular.offsetTop) {
      elevator.querySelector('a[data-name="newGoods"]').classList.add('active')
    } else if (n >= popular.offsetTop && n < brand.offsetTop) {
      elevator.querySelector('a[data-name="popular"]').classList.add('active')
    }else if (n >= brand.offsetTop && n < topic.offsetTop) {
      elevator.querySelector('a[data-name="brand"]').classList.add('active')
    }else if (n >= topic.offsetTop) {
      elevator.querySelector('a[data-name="topic"]').classList.add('active')
     }
  });
})()